cmake_minimum_required(VERSION 3.8)
project(webrtc_package)

# 设置C++标准
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 查找ROS2依赖包
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(std_msgs REQUIRED)
find_package(pcl_conversions REQUIRED)
find_package(pcl_ros REQUIRED)

# 查找其他依赖
find_package(LibDataChannel REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(CURL REQUIRED libcurl)
find_package(Boost REQUIRED COMPONENTS system thread)
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(PCL REQUIRED COMPONENTS common io)

# 查找Draco库 (Google点云压缩库)
find_package(draco QUIET)
if(NOT draco_FOUND)
  message(STATUS "Draco not found via CMake, trying pkg-config...")
  pkg_check_modules(DRACO QUIET draco)
  if(DRACO_FOUND)
    set(draco_FOUND TRUE)
    set(DRACO_LIBRARIES ${DRACO_LIBRARIES})
    set(DRACO_INCLUDE_DIRS ${DRACO_INCLUDE_DIRS})
  else()
    message(WARNING "Draco library not found. Point cloud compression will be disabled.")
    set(draco_FOUND FALSE)
  endif()
endif()

# 包含目录
include_directories(
  src
  ${CURL_INCLUDE_DIRS}
  ${Boost_INCLUDE_DIRS}
  ${PCL_INCLUDE_DIRS}
  /usr/include/websocketpp
  /usr/include/nlohmann
)

# 如果找到Draco，添加其包含目录和编译定义
if(draco_FOUND)
  include_directories(${DRACO_INCLUDE_DIRS})
  add_definitions(-DHAS_DRACO)
  message(STATUS "Draco point cloud compression enabled")
else()
  message(STATUS "Draco point cloud compression disabled")
endif()

# 创建WebRTC客户端库
add_library(webrtc_pub
  src/webrtc_pub.cpp
)

target_link_libraries(webrtc_pub
  LibDataChannel::LibDataChannel 
  OpenSSL::SSL
  ${CURL_LIBRARIES}
  ${Boost_LIBRARIES}
  ${PCL_LIBRARIES}
  Threads::Threads
)

# 如果找到Draco，链接Draco库
if(draco_FOUND)
  if(TARGET draco::draco)
    target_link_libraries(webrtc_pub draco::draco)
  else()
    target_link_libraries(webrtc_pub ${DRACO_LIBRARIES})
  endif()
endif()

target_include_directories(webrtc_pub PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
  $<INSTALL_INTERFACE:include>
)

# 创建ROS2节点可执行文件
add_executable(ros_webrtc_node
  src/ros_webrtc_node.cpp
)

ament_target_dependencies(ros_webrtc_node
  rclcpp
  sensor_msgs
  std_msgs
  pcl_conversions
  pcl_ros
)

target_link_libraries(ros_webrtc_node
  webrtc_pub
  ${PCL_LIBRARIES}
)

# 安装
install(TARGETS webrtc_pub
  EXPORT export_${PROJECT_NAME}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)

install(TARGETS ros_webrtc_node 
  DESTINATION lib/${PROJECT_NAME}
)

# 安装头文件
install(FILES src/webrtc_pub.hpp
  DESTINATION include/${PROJECT_NAME}
)

# 安装启动文件和配置文件
install(DIRECTORY launch/
  DESTINATION share/${PROJECT_NAME}/launch
)

install(DIRECTORY config/
  DESTINATION share/${PROJECT_NAME}/config
)

# 测试
if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

ament_export_include_directories(include)
ament_export_libraries(webrtc_pub)
ament_export_targets(export_${PROJECT_NAME})

ament_package()